package com.siss.tenancy.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.siss.tenancy.exception.IllegalParameterException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * JSON反序列化 Date格式化处理
 * 
 * @author yingw
 * @version 1.0
 */
public class DateJsonDeserializer extends JsonDeserializer<Date> {

	public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	
	@Override
	public Date deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException, JsonProcessingException {
		try  
        {  
            return format.parse(arg0.getText());  
        }  
        catch(Exception e)  
        {  
            System.out.println(e.getMessage());  
            throw new IllegalParameterException("日期格式不合法,正确的日期格式:yyyy-MM-dd HH:mm:ss");
        }   
	}

}
